package com.maegul.data.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.maegul.data.dao.AbstractDAO;
import com.maegul.data.entities.Password;

@Repository(value = "passwordDao")
public class PasswordDaoImpl extends AbstractDAO<Password> implements PasswordDao {

	@PersistenceContext
	private EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.maegul.data.dao.DAO#getEntityManager()
	 */
	public EntityManager getEntityManager() {
		return em;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.maegul.data.dao.DAO#getClazz()
	 */
	public Class<Password> getClazz() {
		return Password.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.maegul.data.dao.impl.UserDao#findByUsername(java.lang.String)
	 */
	public Password findByUsername(String username) {
		Query q = getEntityManager().createQuery("select u from " + getClazz() + " where u.username = :username");
		q.setParameter("username", username);
		
		return (Password) q.getSingleResult();
	}
}
